//
//  TitleButton.swift
//  ZHTQWeibo
//
//  Created by ZHTQ on 2018/9/15.
//  Copyright © 2018年 ZHTQ. All rights reserved.
//

import UIKit

class TitleButton: UIButton {
    
    ///添加一些自定的基本属性
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpUI()
    }
    
    ///自定义初始化
    private func setUpUI(){
        //设置按钮文字颜色和 默认图片
        setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        setImage(#imageLiteral(resourceName: "navigationbar_arrow_down"), for: UIControlState.normal)
        setImage(#imageLiteral(resourceName: "navigationbar_arrow_up"), for: UIControlState.selected)
    }
    
    ///给文字图片加上点间隙
    override func setTitle(_ title: String?, for state: UIControlState) {
        super.setTitle((title ?? "") + "  " , for: state)
    }
    
    
    ///改变内部子空间的布局
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置文字的x 为 0 image的x 为文字的宽度
        titleLabel?.frame.origin.x = 0
        imageView?.frame.origin.x = titleLabel!.frame.width
    }
}
